package com.hxh.component.basicore.Base.app;

import android.app.Application;
import android.os.Build;
import android.os.Environment;
import android.util.ArrayMap;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.android.arouter.utils.TextUtils;
import com.hxh.component.basicore.util.Utils;

import java.io.File;

import me.yokeyword.fragmentation.Fragmentation;

/**
 * 标题: 系统初始化类
 * 作者: hxh
 * 日期: 2017/11/7 18:29
 * 描述: 初始化相关组件
 */
public abstract class AppInitDelegate {
    public AppInitDelegate( Application app) {
        this.mApplication = app;
        initDelegate();
    }


    protected Application mApplication;
    protected String mDatasDir;
    protected String mImagesDir;
    protected ArrayMap<String, String> mSaveData; //SaveData可以看做一个小型数据存储库



    //region -----------------------你必须要实现的a方法-----------------------


    public abstract void init();
    /**
     * 初始化
     */
    public abstract void A_initCoreLib();


    //endregion


    //region -----------------------可选配置方法-----------------------


    public void initSharpe() {

    }

    public String getAppCacheDir() {
        if (null == mDatasDir) {
            mDatasDir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "xakj"+File.separator;
            File file = new File(mDatasDir);
            if (!file.exists()) {
                file.mkdirs();
            }

        }
        return mDatasDir;
    }


    public String getAppImgCacheDir() {
        if (null == mImagesDir) {
            mImagesDir = getAppCacheDir() + File.separator + "img";
            File file = new File(mImagesDir);
            if (!file.exists()) {
                file.mkdirs();
            }

        }
        return mImagesDir;
    }

    //endregion

    //region -----------------------系统必须初始化的，无需干预-----------------------
    private void initDelegate() {

        // Utils.init(mApplication);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mSaveData = new ArrayMap<>();
        }
        A_initCoreLib();
        getAppCacheDir();
        getAppImgCacheDir();
        initArouter();
        initSharpe();

        initFragmention();

    }

    private void initFragmention() {
        Fragmentation.builder()
                .stackViewMode(Fragmentation.BUBBLE)
                .debug(Utils.isDebug())
                .install();
    }


    /**
     * 初始化Arouter
     */
    private void initArouter() {
        if (Utils.isDebug()) {
            ARouter.openLog();     // 打印日志
            ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
            //
        }
        ARouter.init(mApplication);
    }




    //endregion

    public void saveData(String key, String value) {
        if (mSaveData.containsKey(key)) mSaveData.remove(key);
        mSaveData.put(key, value);
    }

    public void removeData(String key)
    {
        if (mSaveData.containsKey(key)) mSaveData.remove(key);
    }

    public String getData(String key) {

        return TextUtils.isEmpty(mSaveData.get(key)) ? "" : mSaveData.get(key);
    }

}
